/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sg.edu.nus.iss.billing.ui.gui;

import java.awt.ItemSelectable;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyVetoException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

import sg.edu.nus.iss.billing.ui.controller.*;
import sg.edu.nus.iss.billing.vo.account.AccountVO;
import sg.edu.nus.iss.billing.vo.customer.CustomerComplaintVO;
import sg.edu.nus.iss.billing.vo.subscription.SubscriptionVO;
/**
 *
 * @author Administrator
 */
public class CustomerComplaintWindow extends javax.swing.JInternalFrame {

    /**
     * Creates new form CustomerComplaintWindow
     */
	private OneBillingSystem onebillingsystem;
	private CustomerComplaintVO customercomplaintvo;
	private AccountVO accviewobj;
	private String accountNumber;
	
    public CustomerComplaintWindow(OneBillingSystem onebillingsystem,String AccountNumber) {
    	this.onebillingsystem =onebillingsystem;
    	this.accountNumber = AccountNumber;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

    	
    	
    	jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtComplaint = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        txtCreatedDate = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        cmbStatus = new javax.swing.JComboBox();
        btnSearch = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtAccountNo = new javax.swing.JTextField();
        cmbComplaintID = new javax.swing.JComboBox();
        btnCancel = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        btnNewComplaint = new javax.swing.JButton();
        
        setClosable(true);
        setTitle("Customer Complaint");
        setLocation(450, 50);
        txtAccountNo.setText(accountNumber);
        
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))));

        jLabel2.setText("Complaint Id");

        jLabel3.setText("Complaint ");

        txtComplaint.setColumns(20);
        txtComplaint.setRows(5);
        jScrollPane1.setViewportView(txtComplaint);
        
        txtCreatedDate.setEditable(false);
        txtAccountNo.setEditable(false);
        jLabel4.setText("Created Date");

        jLabel5.setText("[ dd/mm/yyyy ]");

        jLabel6.setText("Status");

        cmbStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] {"PENDING" , "CLOSED" }));

        btnSearch.setText("Retrieve Complaints");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
           	 	btnSearchPerformed(evt);
            }
        });
        //btnSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sg/edu/nus/iss/billing/ui/gui/images/Field_Lookup.png")));
        
        jLabel1.setText("Account Number");
        
        //cmbComplaintID.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Complaint 1", "Complaint 2", "Complaint 3", "Complaint 4" }));
       
        ItemListener itemListener = new ItemListener() {
			public void itemStateChanged(ItemEvent ie) {
				cmbComplaintIDPerformed(ie);
			}
          };
          cmbComplaintID.addItemListener(itemListener);
        
          ItemListener itemListenerStatus = new ItemListener() {
  			public void itemStateChanged(ItemEvent ie) {
  				
  			}
          };
          cmbStatus.addItemListener(itemListenerStatus);  
        
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel6)
                    .addComponent(jLabel4)
                    .addComponent(jLabel1))
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtCreatedDate, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel5))
                            .addComponent(cmbStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(cmbComplaintID, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(txtAccountNo, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnSearch)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(0, 10, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtAccountNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearch))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cmbComplaintID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCreatedDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(cmbStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10))
        );
        
        
        btnCancel.setText("Close");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	try {
					setClosed(true);
				} catch (PropertyVetoException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });
        
        btnSave.setText("Update");  
        btnSave.setEnabled(false);//Making Default false
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
           	 btnSaveActionPerformed(evt);
            }
        });

        btnNewComplaint.setText("Log as New Complaint");
        btnNewComplaint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
           	 btnNewActionPerformed(evt);
            }
        });
        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnNewComplaint)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSave)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancel))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnSave)
                    .addComponent(btnNewComplaint))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    //Complaint ID binding in combobox
    private void btnSearchPerformed(java.awt.event.ActionEvent evt) {
    	try{
    		
    		customercomplaintvo = new CustomerComplaintVO(txtAccountNo.getText());
    		
    		Iterator<CustomerComplaintVO> iterator = onebillingsystem.getCustomerComplaintID(customercomplaintvo).iterator();
    		  		
    		DefaultComboBoxModel theModel = (DefaultComboBoxModel)cmbComplaintID.getModel();
    		theModel.removeAllElements();
    		
    		if (!iterator.hasNext())
    		{	
    			JOptionPane.showMessageDialog(null,"There are no complaints by this customer","No Complaints found",JOptionPane.INFORMATION_MESSAGE);
    			btnSave.setEnabled(false);
    			return;
    		}
    		else
    		{
    			btnSave.setEnabled(true);
    		}
    		while(iterator.hasNext()){
    			cmbComplaintID.addItem(iterator.next().getComplaintId());
    		}
    		
        } catch (Exception e){
        	
        }
    }
    
    private void cmbComplaintIDPerformed(ItemEvent ie) {
    	try{
    		bindComplaint((String)ie.getItem());
    		
        } catch (Exception e){
        	
        }
    }
    
    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
    	try{
    		customercomplaintvo = new CustomerComplaintVO(txtAccountNo.getText().trim());
    		customercomplaintvo.setComplaintId((String)cmbComplaintID.getSelectedItem());
    		
    		 if ((String)cmbStatus.getSelectedItem() == "Pending") {
    			customercomplaintvo.setStatus(sg.edu.nus.iss.billing.common.Util.COMPLAINT_PENDING);
    		} else if ((String)cmbStatus.getSelectedItem() == "Close") {
    			customercomplaintvo.setStatus(sg.edu.nus.iss.billing.common.Util.COMPLAINT_CLOSED);
    		}
    		System.out.println((String)cmbStatus.getSelectedItem());
    		
    		if(onebillingsystem.updateCustomerComplaint(customercomplaintvo)){
    			JOptionPane.showMessageDialog(null,"Customer complaint update successfully .","Success",JOptionPane.INFORMATION_MESSAGE);
    		} else {
    			JOptionPane.showMessageDialog(null,"Customer complaint cannot save.","Error",JOptionPane.INFORMATION_MESSAGE);
    		}
    		 
        } catch (Exception e){
        	
        }
    }
    
    private void btnNewActionPerformed(java.awt.event.ActionEvent evt){
    	try{
    		customercomplaintvo = new CustomerComplaintVO(txtAccountNo.getText().trim());
    		customercomplaintvo.setComplaint(txtComplaint.getText().trim());
    		
    		if(onebillingsystem.createCustomerComplaint(customercomplaintvo)){
    			JOptionPane.showMessageDialog(null,"New complaint successfully created .","Operation Statue",JOptionPane.INFORMATION_MESSAGE);
    		} else {
    			JOptionPane.showMessageDialog(null,"New complaint creation unsuccessful","Operation Statue",JOptionPane.INFORMATION_MESSAGE);
    		}
    		
    	}catch(Exception e){
    		
    	}
    }
    
    private void statusSelection(String selectedstatus){
    	if (selectedstatus ==sg.edu.nus.iss.billing.common.Util.COMPLAINT_PENDING){
			cmbStatus.setSelectedItem(sg.edu.nus.iss.billing.common.Util.COMPLAINT_PENDING);
		} else if (selectedstatus ==sg.edu.nus.iss.billing.common.Util.COMPLAINT_CLOSED) {
			cmbStatus.setSelectedItem(sg.edu.nus.iss.billing.common.Util.COMPLAINT_CLOSED);

		}
    }
    
    private void bindComplaint(String complaintID){
    	try{
    		System.out.println("Customer Complaint WIN: " + complaintID);
    		
    		customercomplaintvo = new CustomerComplaintVO(txtAccountNo.getText().trim());
    		customercomplaintvo.setComplaintId(complaintID.trim());
    		
    		customercomplaintvo = onebillingsystem.viewCustomerComplaintDetail(customercomplaintvo);
    		
    		
    		txtComplaint.setText(customercomplaintvo.getComplaint());  
    		
    		if (customercomplaintvo.getStatus().toUpperCase().equalsIgnoreCase(sg.edu.nus.iss.billing.common.Util.COMPLAINT_PENDING.toUpperCase())){
    			cmbStatus.setSelectedIndex(0);
    			System.out.println("customercomplaintvo.getStatus().toUpperCase()");
    		} else if (customercomplaintvo.getStatus().toUpperCase().equalsIgnoreCase(sg.edu.nus.iss.billing.common.Util.COMPLAINT_CLOSED.toUpperCase())) {
    			cmbStatus.setSelectedIndex(1);
    			System.out.println("customercomplaintvo.getStatus().toUpperCase()");
    		}
    		System.out.println(customercomplaintvo.getStatus());

    		txtCreatedDate.setText(sg.edu.nus.iss.billing.common.Util.getFormattedDateWithoutTime(customercomplaintvo.getDateCreated()));
    		
    		
    		
    	}catch (Exception e){
    		
    	}
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnSearch;
    private javax.swing.JComboBox cmbComplaintID;
    private javax.swing.JComboBox cmbStatus;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtAccountNo;
    private javax.swing.JTextArea txtComplaint;
    private javax.swing.JTextField txtCreatedDate;
    private javax.swing.JButton btnNewComplaint;
    // End of variables declaration//GEN-END:variables
}
